type guard 型別判斷
- 最基本的類型可以用
typeof
做判斷function getLength(something: string | number): number {
if (typeof something === 'number') {
return something.toString().length
}
return something.length
}
console.log(getLength('123'))
console.log(getLength(123)) - custom type 可以用
in
來做判斷是否為某個 typetype Employee = {
name: string
department: string
}
type Leader = {
name: string
department: string
authority: boolean
}
type UnknownEmployee = Employee | Leader
function work(someone: UnknownEmployee) {
if ('authority' in someone) {
console.log(someone.authority)
return
}
console.log('not leader')
}
const employee: Employee = {
name: 'Tom',
department: 'eng'
}
const leader: Leader = {
name: 'Tom',
department: 'eng',
authority: true
}
work(employee) // not leader
work(leader) // true寫成 class 裡面的
method
同理,可以用in
class SWE {
coding() {
console.log('coding')
}
}
class Manager {
meeting() {
console.log('meeting')
}
}
type Employee = SWE | Manager
function work(employee: Employee) {
if ('coding' in employee) {
employee.coding()
}
if ('meeting' in employee) {
employee.meeting()
}
}
work(new SWE())
class
可以用instanceof
做判斷 注意:type 不能用 instanceof
class SWE {
coding() {
console.log('coding')
}
}
class Manager {
meeting() {
console.log('meeting')
}
}
type Employee = SWE | Manager
function work(employee: Employee) {
if (employee instanceof SWE) {
employee.coding()
}
if (employee instanceof Manager) {
employee.meeting()
}
}
work(new SWE())